SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE PROCEDURE [dbo].[DelayedCompCurrent] AS
BEGIN
SELECT  psf.[Asset ID] ,
        map.SSB_ID ,
        map.FS_ID ,
        psf.[Security Name/Description] ,
        i.Spread ,
        psf.[Trade Date] ,
		DATEADD(DAY, 10, psf.[Trade Date]) AS 'Start_Delay_Comp',
		DATEDIFF(day, DATEADD(DAY, 10, psf.[Trade Date])  , psf.[Contractual Settle Date]) AS 'Delay_Comp_To_Settle',

		CASE WHEN psf.[Transaction Type]  ='SELL' THEN  -1*   i.Spread * ABS( CAST(DATEDIFF(day, DATEADD(DAY, 10, psf.[Trade Date])  , psf.[Contractual Settle Date]) AS DECIMAL(18,5)))* psf.[Shares/Par]/ CAST(360 AS FLOAT) ELSE
    i.Spread * CAST(DATEDIFF(day, DATEADD(DAY, 10, psf.[Trade Date])  , psf.[Contractual Settle Date]) AS DECIMAL(18,5))* psf.[Shares/Par]/ CAST(360 AS FLOAT) 
	END			AS 'Accrued_Interest',
        psf.[Contractual Settle Date] ,
		PSF.[Shares/Par],
        psf.[Transaction Type] ,
		psf.[Fund Name],
		psf.[SSC Trade ID]
FROM    dbo.PSFormulas psf	
        OUTER APPLY ( SELECT    d.FS_ID ,
                                d.SSB_ID
                      FROM      dbo.SSB_CUSIP_Map_ALLTIME d
                      WHERE     psf.[Asset ID] = d.SSB_ID
                    ) map
        CROSS APPLY ( SELECT    _i.InstrName ,
                                _i.Spread
                      FROM      FSFundModel.dbo.Instrument _i
                      WHERE     _i.BaseRateType = 'LIBOR3Month'
                                AND _i.ExtId = map.FS_ID
                    ) i
WHERE   psf.[Actual Settlement Date] IS NULL
        AND LEFT(map.FS_ID, 2) = 'LX'
        AND psf.Canceled <> 'Y'
        AND psf.[Transaction Type] IN ( 'BUY', 'SELL' )
ORDER BY psf.[Trade Date] ASC
END

GO
EXEC sp_addextendedproperty N'VirtualFolder', N'Dbo/QuarterlyReporting/Accounting', 'SCHEMA', N'dbo', 'PROCEDURE', N'DelayedCompCurrent', NULL, NULL
GO
